package com.jsz.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jsz.dao.CheckItemDao;
import com.jsz.entity.PageResult;
import com.jsz.entity.QueryPageBean;
import com.jsz.pojo.CheckItem;
import com.jsz.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional   //开启事务管理
public class CheckItemServiceimpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    //添加数据
    @Override
    public void addCheckItem(CheckItem checkItem) {
        checkItemDao.addCheckItem(checkItem);
    }

    //分页数据查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //可以用来做条件查询
        String queryString = queryPageBean.getQueryString();

        //每页有多少条数据
        Integer pageSize = queryPageBean.getPageSize();

        //当前页码
        Integer currentPage = queryPageBean.getCurrentPage();

        //使用mybatis中的分页插件
        PageHelper.startPage(currentPage, pageSize);

        //调用持久层.根据条件查询数据
        Page<CheckItem> page = checkItemDao.findByCondition(queryString);

        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        return new PageResult(total, rows);
    }

    //根据ID查询数据
    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    //根据ID删除数据
    @Override
    public void deleteById(Integer id) throws RuntimeException {
        long count = checkItemDao.findCountByCheckItemID(id);
        //判断
        if (count > 0) {
            //当前检查项已经关联到检查组,不允许删除
            throw new RuntimeException("当前检查项已经被引用，不能被删除!");
        }
        checkItemDao.deleteById(id);
    }

    //编辑数据
    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    //查询所有
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
